package com.business.log;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.log.LogReply;
import com.sql.DBFactory;

public class LogReplyBus {

	private Connection conn = null;

	private Statement stmt = null;

	private ResultSet rs = null;

	private static final Log log = LogFactory.getLog(LogReplyBus.class);

	public void add(LogReply lr) {
		String nickname = lr.getNickname();
		String replyContent = lr.getReplyContent();
		String replyTime = lr.getReplyTime();
		String logNo = lr.getLogNo();
		String sql = "insert into log_reply(nickname,reply_content,reply_time,log_no) values('";
		sql += nickname + "','" + replyContent + "','" + replyTime + "','"
				+ logNo + "')";
		List list=getReplyByLogNo(logNo);
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			 sql= "update log_info set replynum=" + (list.size()+1)
						+ " where log_no='" + logNo + "'";
			stmt.executeUpdate(sql);
				
		} catch (Exception e) {
			log.error("the insert logreply exception:" + e.getMessage());
			log.error("the insert logreply sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	@SuppressWarnings("unchecked")
	public List getReplyByLogNo(String logNo) {
		List list = new ArrayList();
		String sql = "select * from log_reply where log_no='" + logNo
				+ "' order by reply_time desc";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				LogReply lr = new LogReply();
				lr.setLogNo(logNo);
				lr.setNickname(rs.getString("nickname"));
				lr.setReplyContent(rs.getString("reply_content"));
				lr.setReplyId(rs.getString("reply_id"));
				lr.setReplyTime(rs.getString("reply_time"));
				list.add(lr);
			}
		} catch (Exception e) {
			log.error("the select logreply exception:" + e.getMessage());
			log.error("the select logreply sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	public List getReplyByNickname(String nickmame) {
		List list = new ArrayList();
		String sql = "select * from log_reply where nickname='" + nickmame
				+ "' order by reply_time desc ";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				LogReply lr = new LogReply();
				lr.setLogNo(rs.getString("log_no"));
				lr.setNickname(rs.getString("nickname"));
				lr.setReplyContent(rs.getString("reply_content"));
				lr.setReplyId(rs.getString("reply_id"));
				lr.setReplyTime(rs.getString("reply_time"));
				list.add(lr);
			}
		} catch (Exception e) {
			log.error("the select logreply exception:" + e.getMessage());
			log.error("the select logreply sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	public List getReply(String userName, int num) {
		List list = new ArrayList();
		String sql = "select R.* from log_reply R inner join log_info I on R.log_no=I.log_no where I.log_author='"
				+ userName + "'  order by R.reply_time desc limit 0," + num;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				LogReply lr = new LogReply();
				lr.setLogNo(rs.getString("log_no"));
				lr.setNickname(rs.getString("nickname"));
				lr.setReplyContent(rs.getString("reply_content"));
				lr.setReplyId(rs.getString("reply_id"));
				lr.setReplyTime(rs.getString("reply_time"));
				list.add(lr);
			}
		} catch (Exception e) {
			log.error("the select logreply exception:" + e.getMessage());
			log.error("the select logreply sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

		return list;
	}

	public List getReplyTop5(String userName) {
		return getReply(userName, 5);
	}
	
	public LogReply getReplyById(String replyId) {
		LogReply lr = new LogReply();
		String sql = "select * from log_reply where reply_id=" + replyId;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				lr.setLogNo(rs.getString("log_no"));
				lr.setNickname(rs.getString("nickname"));
				lr.setReplyContent(rs.getString("reply_content"));
				lr.setReplyId(rs.getString("reply_id"));
				lr.setReplyTime(rs.getString("reply_time"));
			}
		} catch (Exception e) {
			log.error("the select logreply exception:" + e.getMessage());
			log.error("the select logreply sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

		return lr;

	}
	public void del(String replyId) {
		String sql = "delete from log_reply where reply_id=" + replyId;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the update logreply exception:" + e.getMessage());
			log.error("the update logreply sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void update(LogReply lr, String replyId) {
		String replyContent = lr.getReplyContent();
		String replyTime = lr.getReplyTime();
		String nickname = lr.getNickname();
		String logNo = lr.getLogNo();
		String sql = "update log_reply set reply_content='" + replyContent
				+ "',reply_time='" + replyTime + "',nickname='" + nickname
				+ "',log_no='" + logNo + "'  where reply_id=" + replyId;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the update logreply exception:" + e.getMessage());
			log.error("the update logreply sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

}
